//
//  FYStatusRetweetView.swift
//  Weibo
//
//  Created by 冯毅 on 16/8/12.
//  Copyright © 2016年 冯毅. All rights reserved.
//

import UIKit
import SnapKit


class FYStatusRetweetView: UIView {

    var retweetViewBottomConstraint: Constraint?
    
    var statusViewModel: FYStatusViewModel? {
        didSet {
            contentLabel.text = statusViewModel?.retweetContent
            
            retweetViewBottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0 {
                //  显示配图
                pictureView.hidden = false
                //  设置配图信息
                pictureView.picUrls = picUrls
                //  更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
            } else {
                //  隐藏配图
                pictureView.hidden = true
                //  更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
        }
        
    }
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.grayColor())
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView: FYStatusPictureView = {
        let view = FYStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //  添加控件
        addSubview(contentLabel)
        addSubview(pictureView)

        //  设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
        }
        
        //  关键约束 让其转发微博的视图底部约束 = 内容底部的约束 + 间距
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }
    
    
}
